package cn.deschen.search.index.adUnitEle.adUnitCreative;

import cn.deschen.search.index.Many2ManyIndexAware;
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @Author hanbin_chen
 * @Description 推广单元与创意广告索引，四级索引
 * @DateTime 2021/6/7
 * @Version V1.0.0
 */
@Component
@EqualsAndHashCode
public class AdUnitCreativeIndex extends Many2ManyIndexAware<Long, Long, Long, AdUnitCreativeObject> {

    public static final Map<Long, AdUnitCreativeObject> storageMap = new ConcurrentHashMap<>();

    /**
     * 映射：推广单元Id -> 创意广告ID集合
     */
    public static final Map<Long, Set<Long>> unitCreativeMap = new ConcurrentHashMap<>();

    /**
     * 映射： 创意广告ID -> 推广单元Id集合
     */
    public static final Map<Long, Set<Long>> creativeUnitMap = new ConcurrentHashMap<>();

    public AdUnitCreativeIndex() {
        super(storageMap, unitCreativeMap, creativeUnitMap);
    }


    /**
     * 推广单元ID
     * @param value
     * @return
     */
    @Override
    public Long getAId(AdUnitCreativeObject value) {
        return value.getUnitId();
    }

    /**
     * 创意广告ID
     * @param value
     * @return
     */
    @Override
    public Long getBId(AdUnitCreativeObject value) {
        return value.getCreativeId();
    }

    public Set<Long> matchUnit(Collection<Long> unitIdSet) {
        return unitIdSet.stream().map(unitId -> unitCreativeMap.get(unitId))
                .flatMap(Collection::stream).collect(Collectors.toSet());
    }
}
